package com.yvon.maple.service.msg.core.service.impl;

import com.yvon.boot.common.result.ResponseData;
import com.yvon.maple.service.msg.core.service.SendService;
import com.yvon.maple.service.msg.core.model.vo.SendDTO;
import com.yvon.maple.service.msg.core.model.vo.BatchSendParam;
import com.yvon.maple.service.msg.core.model.entity.MessageTemplate;
import com.yvon.maple.service.msg.core.common.enums.BusinessCode;
import com.yvon.maple.service.msg.core.common.model.send.SendTaskModel;
import com.yvon.maple.service.msg.core.support.pipeline.ProcessContext;
import com.yvon.maple.service.msg.core.support.pipeline.ProcessController;
import com.yvon.maple.service.msg.core.service.MessageTemplateService;
import com.yvon.maple.service.msg.core.model.vo.MessageVo;
import com.yvon.maple.service.msg.core.model.vo.SendParam;
import com.yvon.maple.service.msg.core.model.vo.SendVo;
import com.yvon.maple.utils.DataConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * 发送接口实现
 *
 * @author : Yvon
 * @since : 2021-12-23
 */
@Service
public class SendServiceImpl implements SendService {

    @Autowired
    private ProcessController processController;

    @Autowired
    private MessageTemplateService templateService;

    @Override
    public SendDTO send(SendVo sendVo) {
        // 查询消息模板
        MessageTemplate messageTemplate = templateService.getByTypeAndChannel(sendVo.getMessageType().getCodeEn(), sendVo.getChannelType());

        MessageVo messageVo = DataConverter.convert(sendVo, MessageVo.class);

        // 构建发送任务
        SendTaskModel sendTaskModel = SendTaskModel.builder()
                .messageTemplateId(messageTemplate.getId())
                .messageVoList(Collections.singletonList(messageVo))
                .build();

        // 组装处理器
        ProcessContext context = ProcessContext.builder()
                .code(BusinessCode.COMMON_SEND.getCode())
                .processModel(sendTaskModel)
                .needBreak(false)
                .response(ResponseData.success()).build();

        ProcessContext process = processController.process(context);
        return new SendDTO(process.getResponse().getCode(), process.getResponse().getMessage());
    }

    @Override
    public SendDTO send(SendParam sendParam) {

        SendTaskModel sendTaskModel = SendTaskModel.builder()
                .messageTemplateId(sendParam.getMessageTemplateId())
                .messageVoList(Collections.singletonList(sendParam.getMessageVo()))
                .build();

        ProcessContext context = ProcessContext.builder()
                .code(sendParam.getCode())
                .processModel(sendTaskModel)
                .needBreak(false)
                .response(ResponseData.success()).build();

        ProcessContext process = processController.process(context);
        return new SendDTO(process.getResponse().getCode(), process.getResponse().getMessage());
    }

    @Override
    public SendDTO batchSend(BatchSendParam batchSendParam) {
        SendTaskModel sendTaskModel = SendTaskModel.builder()
                .messageTemplateId(batchSendParam.getMessageTemplateId())
                .messageVoList(batchSendParam.getMessageVoList())
                .build();

        ProcessContext context = ProcessContext.builder()
                .code(batchSendParam.getCode())
                .processModel(sendTaskModel)
                .needBreak(false)
                .response(ResponseData.success()).build();

        ProcessContext process = processController.process(context);

        return new SendDTO(process.getResponse().getCode(), process.getResponse().getMessage());
    }
}
